import Vue from 'vue'
import router from '../../router'
Vue.http.options.emulateJSON = true
// Vue.$set('Content-Type', 'application/x-www-form-urlencoded')
export const HttpRequest = function (url, options, type = 1) {
  if (type === 1) {
    return new Promise((resolve, reject) => {
      // 创建一个异步的请求
      Vue.http.get(url, options).then((response) => {
        // 响应成功回调
        let body = response.body
        if (body.code === 999) {
          router.push({name: 'login'})
        }
        else {
          resolve(response)
        }
      }, (response) => {
        // 响应错误回调
        console.log('fail', response)
        reject(response)
      }).catch(error => {
        console.log('error', error)
      })
    })
  }
  else {
    return new Promise((resolve, reject) => {
      // 创建一个异步的请求
      Vue.http.post(url, options).then((response) => {
        // 响应成功回调
        let body = response.body
        if (body.code === 999) {
          router.push({name: 'login'})
        }
        else {
          resolve(response)
        }
      }, (response) => {
        // 响应错误回调
        console.log('fail', response)
        reject(response)
      }).catch(error => {
        console.log('error', error)
      })
    })
  }
}
